Repository navigation
Conversation
…get) InMemoryBudget counts tokens, but tokens are a leaky proxy for money: a million tokens differs by two orders of magnitude between haiku-class and frontier models, and the flat cache-read percent only approximates one provider's cache pricing. UsdBudget implements Budget over llm_budget::AgentBudget: every call is priced from a built-in table of 232 models (prompt-cache reads and writes at their real rates) and booked against a hard USD cap enforced with CAS reservations, with an audit log and a kill switch. The token-denominated trait reports micro-USD. - record_usage prices the full cache split at real rates - record (no split) prices every token at the output rate, worst case - construction rejects unknown models instead of silently pricing /usr/bin/bash - 7 unit tests; clippy -D warnings clean; pre-existing ladder doctest failure (needs 'testing' feature) untouched
There was a problem hiding this comment.
Thanks for this, and for saying up front that you maintain llm-budget. Pricing by real cost instead of tokens is a fair point. We can't merge this version, though. Here's why:
CI
rustfmtfails: thecfg'dpub useinlib.rssorts afterterminal_guard, and twoassert_eq!s in the tests need wrapping. Runningcargo fmt --allfixes both.- The branch now conflicts with
maininCargo.lock, because #4 upgraded rmcp to 3.11 and cleaned up the workspace manifests. Rebase ontomainand regenerate the lockfile. (cargo denyandcargo auditwere failing onmainbefore; #4 fixed both, so a rebased branch gets a clean run.)
Micro-USD units don't fit how borg uses Budget
Everything else that touches the trait still counts in tokens:
executor.rs:1298admits parallel branches withadmit_parallel(n, 1_000), which is 1k tokens. With this budget that turns into $0.001 per branch, so admission no longer means anything.BudgetExhausted { needed, remaining }(agent.rs:191,executor.rs:1326) would showneededin tokens next toremainingin micro-USD.SubBudgetover aUsdBudgetparent gives youremaining() = min(child tokens, parent micro-USD). That's also the split the docs suggest for compositions that mix models, so the suggested workaround doesn't work.
One price per budget
Every charge is priced at the single model you pass to the constructor. But borg compositions mix models (each agent has its own model(), and the ladder moves between tiers), and TokenUsage doesn't record which model a call used. So a tree that mixes haiku and a frontier model gets mispriced by the same ~100x this PR is trying to fix. Getting this right means changing the trait so the charge path carries the model. That's a bigger design question than one feature flag.
Dependency / API surface
- This pins
llm-budget = "0.3", but crates.io is already at 0.8.1. It also brings inthiserror1.x,dashmap5 and a secondhashbrown. - borg's public API would expose
llm_budgettypes:inner(),with_price_table(Arc<llm_budget::PriceTable>)andUsdBudgetError::Config(llm_budget::BudgetError). Every 0.x bump ofllm-budgetwould then be a breaking change for borg. - borg's spend caps would depend on a price table maintained outside this repo.
Suggested path
Budget is a public trait, and nothing here needs borg internals. We'd prefer this live on your side, either behind a borg feature in llm-budget or as a small llm-budget-borg crate. We're glad to link it from our docs. If we end up making the budget path model-aware in borg itself (charging per model), we'll open an issue and would welcome your input there.
Minor, if you keep a version of this: PriceTable::builtin().clone() copies the whole table for every budget (an Arc would avoid that). Also, the code sample in the PR description lost its quoting (/usr/bin/bash.25, model name unquoted).
|
Thanks for the detailed read, that all makes sense. The unit mismatch with I'll move this to my side as a |
|
Published as the It keeps borg's units as tokens. You pick a reference model, and each call is charged the reference-model tokens that cost the same as that call did on its own model. So Usage: The tests run it inside real borg v1.0.0 compositions: mixed-model trees, 64 parallel branches across threads, parallel admission, and sub-budget exhaustion. If you open the model-aware budget issue, I'm happy to help there. Putting the model on the charge path would also let this drop the per-thread note it uses to match each charge to its call. |
Problem
InMemoryBudgetcounts tokens, but tokens are a leaky proxy for money:with_cache_read_percentapproximates one provider's cache pricing; OpenAI (50%), Google (25%) and Anthropic (10%) all differ, and cache writes have their own ratesTeams running borg pipelines against real APIs need to say "this composition may not spend more than $0.25", not "…more than N tokens".
Change
New feature-gated module
borg::usd_budget(featureusd-budget, optional depllm-budget = "0.3"):UsdBudgetimplementsBudgetoverllm_budget::AgentBudget:record_usageprices the full usage split (fresh input, cache reads, cache writes, output) from a built-in table of 232 models; custom tables viawith_price_tableAgentBudget; safe to share across parallel branches per the trait contractBudgettrait reports micro-USD (1 USD = 1,000,000 units) — documented on the typerecord()(no usage split) prices every token at the model's output rate so an un-split charge can't silently underdrawinner()exposes the wrappedAgentBudgetfor what the trait has no spelling for (audit log, kill switch, tier cascade)Verification
cargo clippy -p borg --features usd-budget -- -D warningscleancargo test -p borg --features usd-budget,testing— all green. (The one doctest failure under--features usd-budgetalone,ladder.rsneeding thetestingfeature, fails identically on cleanmain.)Disclosure: I'm the author of
llm-budget. It's MIT-licensed, on crates.io, dependency-light, and its price table is maintained against provider pricing pages — happy to adjust the integration shape if you'd rather see e.g. a different unit denomination.